package com.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

/**
 * @Auther: luobiao
 * @Date: 2020/4/19 00:46
 * @Description:
 */
@Configuration
public class MyImportClass implements ImportAware {
    private int max;

    /**
     * 通过实现ImportAware接口，会在{@link ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor}后置处理器
     * 的 postProcessBeforeInitialization 方法中调用setImportMetadata传入AnnotationMetadata，从而能获取到@EnableTest注解的max值
     * @param annotationMetadata
     */
    @Override
    public void setImportMetadata(AnnotationMetadata annotationMetadata) {
        Map<String, Object> attributesMap = annotationMetadata.getAnnotationAttributes(EnableTest.class.getName());
        AnnotationAttributes attrs = AnnotationAttributes.fromMap(attributesMap);
        this.max = attrs.getNumber("max");
        System.out.println("获取到EnableTest中设置的max="+this.max);
    }
}
